﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.ComponentModel;
using System.Globalization;

namespace Xceed.Silverlight.DataGrid.Converters
{
  internal class PrimitiveConverter : TypeConverter
  {
    public PrimitiveConverter( Type type )
    {
      if( type == null )
        throw new ArgumentNullException( "type" );

      if( !this.IsPrimitiveType( type ) )
        throw new ArgumentException( "The specified type is not a primitive type.", "type" );

      this.PrimitiveType = type;
    }

    private Type PrimitiveType
    {
      get;
      set;
    }

    public override bool CanConvertFrom( ITypeDescriptorContext context, Type sourceType )
    {
      if( sourceType == null )
        return false;

      return this.IsPrimitiveType( sourceType );
    }

    public override bool CanConvertTo( ITypeDescriptorContext context, Type destinationType )
    {
      return this.CanConvertFrom( context, destinationType );
    }

    public override object ConvertFrom( ITypeDescriptorContext context, CultureInfo culture, object value )
    {
      if( ( value == null )
        && ( this.PrimitiveType == typeof( string ) ) )
      {
        value = string.Empty;
      }

      if( value != null )
      {
        Type valueType = value.GetType();

        if( this.IsPrimitiveType( valueType ) )
          return Convert.ChangeType( value, this.PrimitiveType, culture );
      }

      throw new InvalidOperationException( "The conversion cannot be performed." );
    }

    public override object ConvertTo( ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType )
    {
      if( destinationType == null )
        throw new ArgumentNullException( "destinationType" );

      if( value != null )
      {
        Type valueType = value.GetType();

        if( ( this.IsPrimitiveType( valueType ) )
          && ( this.IsPrimitiveType( destinationType ) ) )
        {
          if( destinationType == valueType )
            return value;

          return Convert.ChangeType( value, destinationType, culture );
        }
      }

      throw new InvalidOperationException( "The conversion cannot be performed." );
    }

    private bool IsPrimitiveType( Type type )
    {
      return ( type.GetInterface( typeof( IConvertible ).FullName, true ) != null );
    }
  }
}
